const { describe, it } = require("mocha");
const { assert } = require("chai");

const fs = require("fs");
const cases = require("./suites/_cases");

const readdir = fs.readdirSync;

function makeTest(model) {
    if (Array.isArray(model)) {
        return makeTestArray(model);
    }

    const { title, expects, converter } = model;

    return () => {
        describe(title, function() {
            cases.forEach((c, i) => {
                it(`${c} => ${expects[i]}`, () => {
                    assert.strictEqual(converter(c), expects[i]);
                });
            });
        });
    };
}

function makeTestArray(models) {
    const fns = models.map((model) => makeTest(model));
    return () => fns.forEach(fn => fn());
}

function main() {
    const files = readdir(`${__dirname}/suites`);
    files.filter((name) => !/^_/.test(name))
        .map((name) => require(`${__dirname}/suites/${name}`))
        .map((fn) => typeof fn === "function" ? fn : makeTest(fn))
        .forEach((fn) => fn());
}

main();
